<?php
/**
 * search success
 *
 * @package    FinalsClub
 * @subpackage search
 * @author     Masumi
 */
?>
<div id="wrapper" class="search">
	<div id="content">
		<div id="main">
			<div class="main-heading">
				<div class="main-heading">
					<p><?php echo ($where + 1) ?> to 
						<?php if($where + 10 < $size) : ?>
							<?php echo ($where + 10).' ' ?>
						<?php else : ?>
							<?php echo ($size).' ' ?>
						<?php endif; ?>
						out of <?php echo $size ?> results for</p>
					<h1><?php echo $query ?></h1> in <h1><?php echo $type ?></h1>
			</div>

			<?php if($type == "classes") : ?>
			<div class="filter-links with-icon">
				<ul class="courses inline">
					<li>Related Classes</li>
				</ul>
			</div>
			<ul class="courses-list">
				<?php if($results) : ?>
					<?php for($i=0; $i < count($results); $i++) : ?>
						<li>
							<h3>	<?php echo $results[$i]->subject ?>:
								<?php echo link_to($results[$i]->name, 'courses/show?id='.$results[$i]->pk) ?>
							</h3>
							<div class="details">
								<p class="related-users"><strong>Instructor: </strong>
										<?php echo $results[$i]->instructor_name ?>
							</div>
						</li>
					<?php endfor ?>
					<div>
						<p style="text-align: right;">
						<?php if($where > 0) : ?>
							<a href="<?php 	echo "/search/show?type=".Search::COURSE."&query=".$query."&resid=".$resid."&start=0" ?>">
								<img src="/images/first.png" border="0" align="middle" alt="">
							</a>
							<a href="<?php 	echo "/search/show?type=".Search::COURSE."&query=".$query."&resid=".$resid."&start=".($where - 10) ?>">
								<img src="/images/previous.png" border="0" align="middle"  alt="">
							</a>
						<?php endif; ?>
						<span style="vertical-align:middle">
						<?php $count = 1; ?>
						<?php $page = $where / 10 + 1 ?>
						<?php for($i = 2;$i > 0; $i--) : ?>
							<?php if($where - ($i * 10) >= 0) : ?>
								<?php $count++; ?>
								<a href="<?php 	echo "/search/show?type=".Search::COURSE."&query=".$query."&resid=".$resid."&start=".($where - ($i * 10)) ?>" style="text-decoration:none">
									<?php echo ($page - $i) ?>
								</a>&nbsp;
							<?php endif; ?>
						<?php endfor ?>
						<b><?php echo $page ?></b>&nbsp;
						<?php for($i = $count;$i < 5; $i++) : ?>
							<?php 
								if($page + ($i - $count + 1) > (int) ($size / 10)) {
									break; 
								}
							?>
							<a href="<?php 	echo "/search/show?type=".Search::COURSE."&query=".$query."&resid=".$resid."&start=".($where + (($i - $count + 1) * 10)) ?>" style="text-decoration:none">
								<?php echo ($page + ($i - $count + 1)) ?>
							</a>&nbsp;
						<?php endfor ?>
						</span>
						<?php if($next < $size) : ?>
							<a href="<?php 	echo "/search/show?type=".Search::COURSE."&query=".$query."&resid=".$resid."&start=".$next ?>">
								<img src="/images/next.png" border="0" align="middle" alt="">
							</a>
							<a href="<?php 	echo "/search/show?type=".Search::COURSE."&query=".$query."&resid=".$resid."&start=".( ((int) ($size / 10)) * 10) ?>">
								<img src="/images/last.png" border="0" align="middle" alt="">
							</a>
						<?php endif; ?>
						</p>
					</div>
				<?php endif; ?>
			</ul>
			<?php endif; ?>
			
			<?php if($type == "notes") : ?>
			<div class="filter-links with-icon">
				<ul class="book inline">
					<li>Related Class Notes</li>
				</ul>
			</div>
			<ul class="courses-list notes">
				<?php if($results) : ?>
					<?php for($i=0; $i < count($results); $i++) : ?>
						<li>
							<h3> <strong><?php echo  $results[$i]->course ?>:</strong>
								<?php echo link_to($results[$i]->topic, 'notes/show?id='.$results[$i]->pk) ?>
							</h3>
							<p class="date">	
							<?php 
								$datetime = new DateTime($results[$i]->created);
								echo $datetime->format('F j, Y');
							?>
							</p>
						</li>
					<?php endfor ?>
					<div>
						<p style="text-align: right;">
						<?php if($where > 0) : ?>
							<a href="<?php 	echo "/search/show?type=".Search::NOTE."&query=".$query."&resid=".$resid."&start=0" ?>">
								<img src="/images/first.png" border="0" align="middle" alt="">
							</a>
							<a href="<?php 	echo "/search/show?type=".Search::NOTE."&query=".$query."&resid=".$resid."&start=".($where - 10) ?>">
								<img src="/images/previous.png" border="0" align="middle"  alt="">
							</a>
						<?php endif; ?>
						<span style="vertical-align:middle">
						<?php $count = 1; ?>
						<?php $page = $where / 10 + 1 ?>
						<?php for($i = 2;$i > 0; $i--) : ?>
							<?php if($where - ($i * 10) >= 0) : ?>
								<?php $count++; ?>
								<a href="<?php 	echo "/search/show?type=".Search::NOTE."&query=".$query."&resid=".$resid."&start=".($where - ($i * 10)) ?>" style="text-decoration:none">
									<?php echo ($page - $i) ?>
								</a>&nbsp;
							<?php endif; ?>
						<?php endfor ?>
						<b><?php echo $page ?></b>&nbsp;
						<?php for($i = $count;$i < 5; $i++) : ?>
							<?php 
								if($page + ($i - $count + 1) > (int) ($size / 10)) {
									break; 
								}
							?>
							<a href="<?php 	echo "/search/show?type=".Search::NOTE."&query=".$query."&resid=".$resid."&start=".($where + (($i - $count + 1) * 10)) ?>" style="text-decoration:none">
								<?php echo ($page + ($i - $count + 1)) ?>
							</a>&nbsp;
						<?php endfor ?>
						</span>
						<?php if($next < $size) : ?>
							<a href="<?php 	echo "/search/show?type=".Search::NOTE."&query=".$query."&resid=".$resid."&start=".$next ?>">
								<img src="/images/next.png" border="0" align="middle" alt="">
							</a>
							<a href="<?php 	echo "/search/show?type=".Search::NOTE."&query=".$query."&resid=".$resid."&start=".( ((int) ($size / 10)) * 10) ?>">
								<img src="/images/last.png" border="0" align="middle" alt="">
							</a>
						<?php endif; ?>
						</p>
					</div>	
				<?php endif; ?>
			</ul>
			<?php endif; ?>
			<?php if($type == "books") : ?>
			<div class="filter-links with-icon">
				<ul class="annotations inline">
					<li>Related Books</li>
				</ul>
			</div>
			<ul class="annotations-list">
				<?php if($results) : ?>
					<?php for($i=0; $i < count($results); $i++) : ?>
						<li>
							<h4><?php echo link_to(stripslashes($results[$i]->title), 'books/showTOC?id='.$results[$i]->pk) ?>
							<span>by <?php echo stripslashes($results[$i]->author) ?></h4>
						</li>
					<?php endfor ?>
					<div>
						<p style="text-align: right;">
						<?php if($where > 0) : ?>
							<a href="<?php 	echo "/search/show?type=".Search::BOOK."&query=".$query."&resid=".$resid."&start=0" ?>">
								<img src="/images/first.png" border="0" align="middle" alt="">
							</a>
							<a href="<?php 	echo "/search/show?type=".Search::BOOK."&query=".$query."&resid=".$resid."&start=".($where - 10) ?>">
								<img src="/images/previous.png" border="0" align="middle"  alt="">
							</a>
						<?php endif; ?>
						<span style="vertical-align:middle">
						<?php $count = 1; ?>
						<?php $page = $where / 10 + 1 ?>
						<?php for($i = 2;$i > 0; $i--) : ?>
							<?php if($where - ($i * 10) >= 0) : ?>
								<?php $count++; ?>
								<a href="<?php 	echo "/search/show?type=".Search::BOOK."&query=".$query."&resid=".$resid."&start=".($where - ($i * 10)) ?>" style="text-decoration:none">
									<?php echo ($page - $i) ?>
								</a>&nbsp;
							<?php endif; ?>
						<?php endfor ?>
						<b><?php echo $page ?></b>&nbsp;
						<?php for($i = $count;$i < 5; $i++) : ?>
							<?php 
								if($page + ($i - $count + 1) > (int) ($size / 10)) {
									break; 
								}
							?>
							<a href="<?php 	echo "/search/show?type=".Search::BOOK."&query=".$query."&resid=".$resid."&start=".($where + (($i - $count + 1) * 10)) ?>" style="text-decoration:none">
								<?php echo ($page + ($i - $count + 1)) ?>
							</a>&nbsp;
						<?php endfor ?>
						</span>
						<?php if($next < $size) : ?>
							<a href="<?php 	echo "/search/show?type=".Search::BOOK."&query=".$query."&resid=".$resid."&start=".$next ?>">
								<img src="/images/next.png" border="0" align="middle" alt="">
							</a>
							<a href="<?php 	echo "/search/show?type=".Search::BOOK."&query=".$query."&resid=".$resid."&start=".( ((int) ($size / 10)) * 10) ?>">
								<img src="/images/last.png" border="0" align="middle" alt="">
							</a>
						<?php endif; ?>
						</p>
					</div>	
				<?php endif; ?>
			</ul>
			<?php endif; ?>
			<?php if($type == "annotations") : ?>
			<div class="filter-links with-icon">
				<ul class="annotations inline">
					<li>Related Annotations</li>
				</ul>
			</div>
			<ul class="annotations-list">
				<?php if($results) : ?>
					<?php for($i=0; $i < count($results); $i++) : ?>
						<?php $annote = AnnotationPeer::retrieveByPK($results[$i]->pk); ?>
						<li>
							<?php if($annote->getSourceType() == AnnotationPeer::NOTE_SOURCE) : ?>
								<?php $note = NotePeer::retrieveByPK($annote->getNoteId()) ?>
								<?php $course = CoursePeer::retrieveByPK($note->getCourseId()) ?>
								<h4>
									<?php 
										if(EntitlementPeer::isEntitled($note->getId(), $whoami, EntitlementPeer::NOTE_TYPE)) {
											echo link_to(stripslashes($note->getTopic()), 'notes/show?id='.$note->getId());
										} else {
											echo stripslashes($note->getTopic());
										}
									?>				
								<?php $who = UserPeer::retrieveByPK($note->getUserId()) ?>
								<span>by <?php echo stripslashes($who->getUserName()) ?></span></h4>
							<?php elseif($annote->getSourceType() == AnnotationPeer::BOOK_SOURCE) : ?>
								<?php $booksection = SectionPeer::retrieveByPK($annote->getBookSectionId()) ?>
								<?php $book = BookPeer::retrieveByPK($booksection->getBookId()) ?>
								<h4><?php echo link_to(stripslashes($book->getTitle()), 'books/showTOC?id='.$book->getId()) ?>
								<span>by <?php echo stripslashes($book->getAuthor()) ?></h4></span>
								<div class="inner">
									<blockquote><cite>
									<?php echo link_to(stripslashes($booksection->getName()), 'books/showSection?id='.$booksection->getId().'&bookid='.$book->getId()) ?>
									</cite></blockquote>
								</div>
							<?php elseif($annote->getSourceType() == AnnotationPeer::FILE_SOURCE) : ?>
								<?php $file = FilePeer::retrieveByPK($annote->getFileId()) ?>
								<h4>
								<?php 
									if(EntitlementPeer::isEntitled($file->getId(),$whoami,EntitlementPeer::FILE_TYPE)) {
										echo link_to($file->getName(), $file->getAnnotateFileViewURL());
									} else {
										echo $file->getName();
									}
								?>
								<?php $who = UserPeer::retrieveByPK($file->getOwnerId()) ?>
								<span>by <?php echo stripslashes($who->getUserName()) ?></h4>
							<?php else : ?>
								<h4><?php echo stripslashes($results[$i]->subject) ?></h4>
							<?php endif; ?>
							<div class="inner">
								<blockquote>
								<?php 
									if(strlen(stripslashes($results[$i]->text)) > 160) {
										echo htmlspecialchars(substr(stripslashes($results[$i]->text),0,155),ENT_NOQUOTES) . ' ....';
									} else {
										echo stripslashes($results[$i]->text);
									}
								?>
								</blockquote>
							</div>
						</li>
					<?php endfor ?>
					<div>
						<p style="text-align: right;">
						<?php if($where > 0) : ?>
							<a href="<?php 	echo "/search/show?type=".Search::ANNOTATION."&query=".$query."&resid=".$resid."&start=0" ?>">
								<img src="/images/first.png" border="0" align="middle" alt="">
							</a>
							<a href="<?php 	echo "/search/show?type=".Search::ANNOTATION."&query=".$query."&resid=".$resid."&start=".($where - 10) ?>">
								<img src="/images/previous.png" border="0" align="middle"  alt="">
							</a>
						<?php endif; ?>
						<span style="vertical-align:middle">
						<?php $count = 1; ?>
						<?php $page = $where / 10 + 1 ?>
						<?php for($i = 2;$i > 0; $i--) : ?>
							<?php if($where - ($i * 10) >= 0) : ?>
								<?php $count++; ?>
								<a href="<?php 	echo "/search/show?type=".Search::ANNOTATION."&query=".$query."&resid=".$resid."&start=".($where - ($i * 10)) ?>" style="text-decoration:none">
									<?php echo ($page - $i) ?>
								</a>&nbsp;
							<?php endif; ?>
						<?php endfor ?>
						<b><?php echo $page ?></b>&nbsp;
						<?php for($i = $count;$i < 5; $i++) : ?>
							<?php 
								if($page + ($i - $count + 1) > (int) ($size / 10)) {
									break; 
								}
							?>
							<a href="<?php 	echo "/search/show?type=".Search::ANNOTATION."&query=".$query."&resid=".$resid."&start=".($where + (($i - $count + 1) * 10)) ?>" style="text-decoration:none">
								<?php echo ($page + ($i - $count + 1)) ?>
							</a>&nbsp;
						<?php endfor ?>
						</span>
						<?php if($next < $size) : ?>
							<a href="<?php 	echo "/search/show?type=".Search::ANNOTATION."&query=".$query."&resid=".$resid."&start=".$next ?>">
								<img src="/images/next.png" border="0" align="middle" alt="">
							</a>
							<a href="<?php 	echo "/search/show?type=".Search::ANNOTATION."&query=".$query."&resid=".$resid."&start=".( ((int) ($size / 10)) * 10) ?>">
								<img src="/images/last.png" border="0" align="middle" alt="">
							</a>
						<?php endif; ?>
						</p>
					</div>	
				<?php endif; ?>
			</ul>
			<?php endif; ?>
		</div>
	</div>
</div>
